<?php

class Default_Model_DbTable_Categories extends Zend_Db_Table_Abstract
{

    protected $_name = 'categories';

    /**
     * Get category with specific id
     * @param unknown_type $id
     * @throws Exception
     * @return List of category
     */
    public function getCategory($id){
    	$id = (int)$id;
    	$row = $this->fetchRow('id = ' . $id);
    	if (!$row) {
    		throw new Exception("Could not find row $id");
    	}
    	return $row->toArray();
    }
    
    /**
     * Add new category
     * @param unknown_type $category_name
     * @param unknown_type $category_parent
     * @param unknown_type $category_status
     * @param unknown_type $category_order
     * @return The primary key of the row inserted.
     */
    public function addCategory($category_name,$category_parent,$category_status,$category_order){
    	$data = array(
    			'category_name' => $category_name,
    			'category_parent' => $category_parent,
    			'category_status' => $category_status,
    			'category_order' => $category_order
    	);
    	 
    	return $this->insert($data);
    }
    
    /**
     * Delete a category with its id
     * @param unknown_type $id
     * @return number of row deleted
     */
    public function deleteCategory($id){
    	return $this->delete("ID = $id");
    }
    
    
    /**
     * Update a category with specific id
     * @param unknown_type $ID
     * @param unknown_type $category_name
     * @param unknown_type $category_parent
     * @param unknown_type $category_status
     * @param unknown_type $category_order
     * @return number of rows deleted
     */
    public function updateCategory($ID,$category_name,$category_parent,$category_status,$category_order){
    	$data = array(
    			'category_name' => $category_name,
    			'category_parent' => $category_parent,
    			'category_status' => $category_status,
    			'category_order' => $category_order
    	);
    	 
    	return $this->update($data, "ID = $ID");
    }
    
    /**
     * Get category with specific parent id
     * @param unknown_type $id
     * @throws Exception
     * @return array of category
     */
    public function getByParent($id){
    	$id = (int)$id;
    	$rows = $this->fetchAll('category_parent = ' . $id);
    	if (!$rows) {
    		throw new Exception("Could not find");
    	}
    	return $rows->toArray();
    }
}

